package com.qfedu.zf.my.service.impl;
import com.qfedu.zf.common.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.zf.common.utils.OssUtil;
import com.qfedu.zf.my.entity.User;
import com.qfedu.zf.my.mapper.MyMapper;
import com.qfedu.zf.my.mapper.RentMapper;
import com.qfedu.zf.my.service.MyService;
import com.qfedu.zf.my.vo.RentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;
import java.io.IOException;

@Service
public class MyServiceImpl implements MyService {
    @Autowired
    MyMapper myMapper;
    @Autowired
    RentMapper rentMapper;

    @Override
    public User selectById(Integer id) {
        User user = myMapper.selectById(id);
        return user;
    }

    //    上传头像
    @Override
    public String setPicture(MultipartFile file,Integer id) {
        User user = myMapper.selectById(id);
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + filename.substring(filename.lastIndexOf("."));
        String oss = null;
        try {
            oss = OssUtil.uploadFile2OSS(file.getInputStream(), filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setAvatar(oss);
        myMapper.updateById(user);
        return oss;
    }
    //    只上传头像
    @Override
    public String setP(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + filename.substring(filename.lastIndexOf("."));
        String oss = null;
        try {
            oss = OssUtil.uploadFile2OSS(file.getInputStream(), filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return oss;
    }

    @Override
    public R updateName(User user) {
        myMapper.updateById(user);
        return R.ok();
    }
//fabu房源
    @Override
    public R publish(RentVo rentVo) {
        int insert = rentMapper.insert(rentVo);
        return R.ok();
    }

    @Override
    public R selectPublish(Integer id) {
        QueryWrapper<RentVo> rentVoQueryWrapper = new QueryWrapper<>();
        rentVoQueryWrapper.eq("user_id",id);
        RentVo rentVo = rentMapper.selectOne(rentVoQueryWrapper);
        System.out.println("oooo");
        return R.ok(rentVo);
    }

    @Override
    public R deleteOne(Integer id) {
        int i = rentMapper.deleteById(id);
        return R.ok(i);
    }
}
